package com.cyss.greenball.main;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

import com.cyss.greenball.dao.SaveMsgDao;
import com.cyss.greenball.util.ActivityUtil;
import com.cyss.greenball.util.BaseService;
import com.cyss.greenball.util.SystemUtil;
import com.cyss.greenball.view.MyMapViewFactory;
import com.lianzt.commondata.AbstractCommonData;
import com.lianzt.commondata.DataConvertFactory;
import com.lianzt.util.Base64;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DownloadMapActivity extends BaseActivity{

	private TextView myMapTitle;
	private ImageView myMapView;
	private Button myMapPrevious;
	private Button myMapNext;
	private Button downloadMap;
	private Map<String, AbstractCommonData> mapInfo;
	private int nowMapNum = 0;
	private Object[] keyArray;
	private ProgressDialog pdUpload;// 登录圆形进度条

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.my_map);
	}

	@Override
	protected void init() {
		myMapTitle = (TextView) findViewById(R.id.my_map_title);
		myMapView = (ImageView) findViewById(R.id.my_map_view);
		myMapPrevious = (Button) findViewById(R.id.my_map_previous);
		myMapNext = (Button) findViewById(R.id.my_map_next);
		downloadMap = (Button) findViewById(R.id.upload_map);
		downloadMap.setText("保存地图");
		pdUpload = new ProgressDialog(this);
		pdUpload.setMessage("正在下载。。");
		pdUpload.show();
		AbstractCommonData sendData = SystemUtil
			.getSendInstance(SystemUtil.GET_GREENBALL_MAP);
		sendData.putObjectValue("listener", new BaseService.ServiceListener() {

			@Override
			public void finishSendData(AbstractCommonData backAcd) {
				mapInfo = new HashMap<String, AbstractCommonData>();
				System.out.println(backAcd);
				for(Object object : backAcd.getArrayValue("mapList")){
					AbstractCommonData acd = (AbstractCommonData) object;
					mapInfo.put(acd.getStringValue("map_name"), acd);
				}
				pdUpload.cancel();
			}
		});
		BaseService.addService(sendData);
		
	}

	private void setButtonState() {
		if ((nowMapNum == 0 && mapInfo.size() == 0)||(nowMapNum == 0 && mapInfo.size() == 1)) {
			myMapPrevious.setEnabled(false);
			myMapNext.setEnabled(false);
		} else if (nowMapNum == 0 && mapInfo.size() > 1) {
			myMapNext.setEnabled(true);
			myMapPrevious.setEnabled(false);
		} else if (mapInfo == null || mapInfo.size() == 0
				|| nowMapNum == mapInfo.size() - 1) {
			myMapNext.setEnabled(false);
			myMapPrevious.setEnabled(true);
		} else {
			myMapPrevious.setEnabled(true);
			myMapNext.setEnabled(true);
		}
	}

	private void setMyMapView() {
		myMapView.setImageBitmap(MyMapViewFactory.getBitmapByCommdata(
				SystemUtil.SCREEN_WIDTH - 40, SystemUtil.SCREEN_HEIGHT - 60,
				mapInfo.get(keyArray[nowMapNum])));
		myMapTitle.setText(keyArray[nowMapNum] + "(" + (nowMapNum + 1) + "/"
				+ mapInfo.size() + ")");
	}

	private void saveMyMap() {
		SaveMsgDao.putValue(this, mapInfo.get(keyArray[nowMapNum]).getStringValue("map_name") , mapInfo.get(keyArray[nowMapNum]));
		Toast.makeText(this, "保存成功！", Toast.LENGTH_LONG).show();
	}

	@Override
	protected void setOnListener() {
		pdUpload.setOnCancelListener(new DialogInterface.OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				if (mapInfo.size() > 0) {
					keyArray = mapInfo.keySet().toArray();
					setMyMapView();
				} else {
					myMapTitle.setText("服务器暂无地图");
				}
				setButtonState();
			}
		});
		myMapPrevious.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				nowMapNum--;
				setButtonState();
				setMyMapView();
			}
		});

		myMapNext.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				nowMapNum++;
				setButtonState();
				setMyMapView();
			}
		});

		downloadMap.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				saveMyMap();
			}
		});

		myMapView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				AbstractCommonData dialog = DataConvertFactory
						.getInstanceEmpty();
				dialog.putStringValue("text", "是否试玩？");
				dialog.putStringValue("button1", "否");
				dialog.putStringValue("button2", "是");
				dialog.putObjectValue("button2_listener",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Intent intent = new Intent(DownloadMapActivity.this,
										WorldActivity.class);
								intent.putExtra("acdGame", DataConvertFactory
										.praseString(mapInfo
												.get(keyArray[nowMapNum])));
								startActivity(intent);
							}
						});
				ActivityUtil.Dialog(DownloadMapActivity.this, dialog);
			}
		});
	}

}
